<?php 
$weekarr = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$week = $weekarr[date("w")];
?>
<script type="text/javascript">
    function readURL(input,id) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                $('#'+id).html(input.value);
            };
            reader.readAsDataURL(input.files[0]);
        }
    }
</script>
<style>
.input-div table tr{display:block;}    
</style>
<div class="positiondiv">
    <table>
        <tr>
            <td class="td-left">
                <img src="<?=base_url()?>resource/img/home_ico.png" width="21" height="17">
                Data Entry > <a href="<?=base_url()?>index/index">Candidate</a> > New Candidate
            </td>
            <td class="td-right">
                <img src="<?=base_url()?>resource/img/menu2.png" width="27" height="24">Today is <?=$week?>,<?=date('M d,Y')?>
            </td>
        </tr>
    </table>
</div>
<div class="input-div">
    <!--<form action="<?=base_url('index/new_candidate')?>" enctype="multipart/from-data" method="post" onsubmit="return checkform()">-->
    <?php echo form_open_multipart('index/new_candidate');?>
        <table>
            <tr><td></td><td><span class="err"><?=validation_errors();?><?php if(isset($error)){echo $error;}?></span></td></tr>
            <tr><td colspan="2"><h3>.Candidate</h3></td></tr>
            <tr>
                <td colspan="2" class="uploadtd">
                    <a>SELETE A FILE</a><label id="workericlabel">Please upload the worker IC.</label>
                    <input type="file" id="workeric" class="inputfile" name="workeric" size="20" 
                           onchange="readURL(this,'workericlabel');"/>
                    <a href="javascript:void(0)" class="delete" id="workericdelete">
                        <img src="<?=base_url()?>resource/img/delete.png"/>
                    </a>
                </td>
            </tr>
            <tr>
                <td colspan="2" class="uploadtd">
                    <a>SELETE A FILE</a><label id="resumelabel">Please upload the worker resume.</label>
                    <input type="file" id="resume" class="inputfile" name="resume" size="20" 
                           onchange="readURL(this,'resumelabel');"/>
                    <a href="javascript:void(0)" class="delete" id="resumedelete">
                        <img src="<?=base_url()?>resource/img/delete.png"/>
                    </a>
                </td>
            </tr>
            <tr>
                <td colspan="2" class="uploadtd">
                    <a>SELETE A FILE</a><label id="diplomalabel">Please upload the worker education cert.</label>
                    <input type="file" id="diploma" class="inputfile" name="diploma" size="20" 
                           onchange="readURL(this,'diplomalabel');"/>
                    <a href="javascript:void(0)" class="delete" id="diplomadelete">
                        <img src="<?=base_url()?>resource/img/delete.png"/>
                    </a>
                </td>
            </tr>
            <tr>
                <td colspan="2" class="uploadtd">
                    <a>SELETE A FILE</a><label id="passportlabel">Please upload the worker passport.</label>
                    <input type="file" id="passport" class="inputfile" name="passport" size="20" 
                           onchange="readURL(this,'passportlabel');"/>
                    <a href="javascript:void(0)" class="delete" id="passportdelete">
                        <img src="<?=base_url()?>resource/img/delete.png"/>
                    </a>
                </td>
            </tr>
            <tr>
                <td class="nametd">Nationality:</td>
                <td>
                    <input type="radio" name="nationality" value="1" id="Malaysia"/><label for="Malaysia">Malaysia</label>
                    <input type="radio" name="nationality" checked="checked" value="2" id="China"/><label for="China">China</label>
                    <input type="radio" name="nationality" value="3" id="Vietnam"/><label for="Vietnam">Vietnam</label>
                    <input type="radio" name="nationality" value="4" id="Myanmar"/><label for="Myanmar">Myanmar</label>
                    <input type="radio" name="nationality" value="6" id="Other"/>
                    <input type="text" id="othernationality" name="othernationality" placeholder="Other" style="width:100px;" />
                    <span id="nationalitymsg" class="err"></span>
                </td>
            </tr>
            <tr><td class="nametd">Name:</td><td><input type="text" name="name"/><span id="namemsg" class="err">*</span></td></tr>
            <tr>
                <td class="nametd">Intermediary:</td>
                <td>
                    <select name="companyid" id="companyid" style="width:200px;">
                        <option value="0">-SELECT-
                        <?php
                            for($i = 1;$i < count($intermediary);$i++){
                                echo "<option value='".$intermediary[$i]['id']."'>".$intermediary[$i]['name'];
                            }
                        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td class="nametd">Gender:</td>
                <td>
                    <input type="radio" name="gender" checked="checked" value="1" id="man"/><label for="man">M</label>
                    <input type="radio" name="gender" value="2" id="female"/><label for="female">F</label>
                </td>
            </tr>
            <tr>
                <td class="nametd">Birthday:</td>
                <td>
                    <select name="month" id="month">
                        <option value="0">-Month-
                        <?php
                            for($i = 1;$i < 13;$i++){
                                echo "<option value='".$i."'>".$i;
                            }
                        ?>
                    </select>
                    <select name="day" id="day">
                        <option value="0">-Day-
                        <?php
                            for($i = 1;$i < 32;$i++){
                                echo "<option value='".$i."'>".$i;
                            }
                        ?>
                    </select>
                    <select name="year" id="year">
                        <option value="0">-Year-
                        <?php
                            for($i = 1950;$i < 2017;$i++){
                                echo "<option value='".$i."'>".$i;
                            }
                        ?>
                    </select>
                </td>
            </tr>
            <tr><td class="nametd">Expect Position:</td><td><input type="text" name="position"/></td></tr>
            <tr><td class="nametd">Speciality:</td><td><input type="text" name="speciality"/></td></tr>
            <tr>
                <td class="nametd">Location:</td>
                <td>
                    <input type="radio" name="location" checked="checked" value="0" id="Oversea"/>
                    <label for="Oversea">Overseas</label>
                    <input type="radio" name="location" value="1" id="Singapore"/>
                    <label for="Singapore">Singapore</label>
                </td>
            </tr>
            <tr>
                <td class="nametd">Status:</td>
                <td>
                    <input type="radio" name="status" checked="checked" value="0" id="applicable"/>
                    <label for="applicable">Applicable</label>
                    <input type="radio" name="status" value="1" id="NA"/>
                    <label for="NA">N.A.</label>
                </td>
            </tr>
            <tr style="display:none;" id="passtypetr">
                <td class="nametd">Pass Type:</td>
                <td>
                    <select name="passtype" id="passtype">
                        <option value="1">WP
                        <option value="2">SP
                        <option value="3">EP
                        <option value="4">SVP
                        <option value="5">DP
                        <option value="6">Other
                    </select>
                </td>
            </tr>
            <tr><td class="nametd">Comments:</td>
                <td>
                    <textarea name="comment"></textarea>
                </td>
            </tr>
            <tr><td class="nametd">Ever been SGP:</td>
                <td>
                    <input type="radio" name="overbeensgp" value="0" id="syes" checked="checked" /><label for="syes">Yes</label>
                <input type="radio" name="overbeensgp" value="1" id="sno"/><label for="sno">No</label>
                </td>
            </tr>
            <tr><td class="nametd">Interviewer:</td><td><label><?=$this->session->userdata('user')->username?></label></td></tr>
            <tr><td></td><td><input type="submit" value="Save"/><input type="button" value="Reset"/></td></tr>
        </table>
    </form>
</div>
<script>
function checkform(){
    var flag = true;
    var name = $.trim($("input[name='name']").val());
    if(name == ''){
        flag = false;
        $("#namemsg").html("Candidate name can not be null!");
    }else{
        $("#namemsg").html("*");
    }
    
    if($("input[name='nationality']:checked").val() == 6 && $.trim($("#othernationality").val()) == ''){
        flag = false;
        $("#nationalitymsg").html("Please input other nationality!");
    }else{
        $("#nationalitymsg").html('');
    }
    return flag;
}

$("input[type='button']").click(function(){
    $("#resumelabel").html("Please upload the works resume.");
    $("#diplomalabel").html("Please upload the works diploma.");
    $("#passportlabel").html("Please upload the works passport.");
    $("input[name='name']").val('');
    $("#China").attr("checked","checked");
    $("#man").attr("checked","checked");
    $("#month").val(0);
    $("#day").val(0);
    $("#year").val(0);
    $("input[name='position']").val('');
    $("input[name='speciality']").val('');
    $("#shorlist_in_singapore").attr("checked","checked");
    $("textarea[name='comment']").val('');
    var resume = $("#resume") ;
    resume.after(resume.clone().val(""));      
    resume.remove();
    var diploma = $("#diploma") ;
    diploma.after(diploma.clone().val(""));      
    diploma.remove();
    var passport = $("#passport") ;
    passport.after(passport.clone().val(""));      
    passport.remove();
});

$("input[name='location']").click(function(){
    if($("input[name='location']:checked").val() == 1){
        $("#passtypetr").css("display","block");
    }else{
        $("#passtypetr").css("display","none");
    }
});

$(".delete").click(function(){
    var id = $(this).attr("id");
    var inputid = id.split("delete")[0];
    var file = $("#"+inputid) 
    file.after(file.clone().val("")); 
    file.remove(); 
    if(inputid == "workeric"){
        $("#"+inputid+"label").html("Please upload the worker IC.");
    }else if(inputid == "resume"){
        $("#"+inputid+"label").html("Please upload the worker resume.");
    }else if(inputid == "diploma"){
        $("#"+inputid+"label").html("Please upload the worker education cert.");
    }else if(inputid == "passport"){
        $("#"+inputid+"label").html("Please upload the worker passport.");
    }
});
</script>
